package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysWhitePaperChapterMapper;
import org.springframework.transaction.annotation.Transactional;

/**
 * 白皮书章节Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-20
 */
@Service
public class SysWhitePaperChapterServiceImpl implements ISysWhitePaperChapterService
{
    @Autowired
    private SysWhitePaperChapterMapper sysWhitePaperChapterMapper;
    @Autowired
    private ISysWhitePaperOutlineService sysWhitePaperOutlineService;
    @Autowired
    private ISysWhitePaperSurveyTaskService sysWhitePaperSurveyTaskService;
    @Autowired
    private ISysWhitePaperRequireService sysWhitePaperRequireService;
    @Autowired
    private ISysWhitePaperProjectService sysWhitePaperProjectService;
    @Autowired
    private ISysWhitePaperKnowledgeBaseService sysWhitePaperKnowledgeBaseService;

    /**
     * 查询白皮书章节
     *
     * @param id 白皮书章节主键
     * @return 白皮书章节
     */
    @Override
    public SysWhitePaperChapter selectSysWhitePaperChapterById(Long id)
    {
        return sysWhitePaperChapterMapper.selectSysWhitePaperChapterById(id);
    }

    /**
     * 查询白皮书章节列表
     *
     * @param sysWhitePaperChapter 白皮书章节
     * @return 白皮书章节
     */
    @Override
    public List<SysWhitePaperChapter> selectSysWhitePaperChapterList(SysWhitePaperChapter sysWhitePaperChapter)
    {
        return sysWhitePaperChapterMapper.selectSysWhitePaperChapterList(sysWhitePaperChapter);
    }

    /**
     * 新增白皮书章节
     *
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertSysWhitePaperChapter(List<SysWhitePaperChapter> whitePaperChapterList)
    {
        Long projectId = whitePaperChapterList.get(0).getProjectId();
        String loginUserName = SecurityUtils.getUsername();
        Date currentDate = DateUtils.getNowDate();

        // 1,根据项目ID删除章节信息
        sysWhitePaperChapterMapper.deleteSysWhitePaperChapterByProjectId(projectId);

        // 2,根据项目ID删除标题信息
        sysWhitePaperOutlineService.deleteSysWhitePaperOutlineByProjectId(projectId);

        // 3,根据项目ID删除调研任务信息
        sysWhitePaperSurveyTaskService.deleteSysWhitePaperSurveyTaskByProjectId(projectId);

        // 4,根据项目ID获取项目信息
        SysWhitePaperRequire whitePaperRequire = sysWhitePaperRequireService.getRequireAndInsdutryByProjectId(projectId);

        for (int i=0;i<whitePaperChapterList.size();i++) {
            SysWhitePaperChapter whitePaperChapter = whitePaperChapterList.get(i);
            // 5,保存章节数据
            whitePaperChapter.setCreateBy(loginUserName);
            whitePaperChapter.setCreateTime(currentDate);
            whitePaperChapter.setSort(i);
            sysWhitePaperChapterMapper.insertSysWhitePaperChapter(whitePaperChapter);

            // 6,保存章节下的标题及相关内容
            List<SysWhitePaperOutline> outlineList = whitePaperChapter.getOutlineList();
            for (int j=0;j<outlineList.size();j++) {
                SysWhitePaperOutline outline = outlineList.get(j);

                // 组装数据来源信息
                SysWhitePaperOutline.DataSourceInfo dataSourceInfo = outline.getDataSourceInfo();
                String dataSourceStr = "";
                if (StringUtils.isNotEmpty(dataSourceInfo.getWebsites())) {
                    dataSourceStr = dataSourceInfo.getWebsites();
                }
                List<SysWhitePaperKnowledgeBase> knowledgeBaseList = dataSourceInfo.getKnowledgeBaseList();
                if (CollectionUtils.isNotEmpty(knowledgeBaseList)) {
                    List<String> fileNameList = knowledgeBaseList.stream().map(knowledge -> knowledge.getOriginFileName()).collect(Collectors.toList());

                    dataSourceStr += "," + String.join(",",fileNameList);

                    // 保存文件至知识库
                    knowledgeBaseList.stream().forEach(knowledge -> {
                        knowledge.setProjectId(projectId);
                    });
                    sysWhitePaperKnowledgeBaseService.batchInsertSysWhitePaperKnowledgeBase(knowledgeBaseList);
                }

                outline.setSort(j);
                outline.setProjectId(whitePaperChapter.getProjectId());
                outline.setChapterId(whitePaperChapter.getId());
                outline.setDataSource(dataSourceStr);
                outline.setCreateBy(loginUserName);
                outline.setCreateTime(currentDate);


            }
            sysWhitePaperOutlineService.batchInsertSysWhitePaperOutline(outlineList);

            // 7,保存标题相关的调研任务
            sysWhitePaperSurveyTaskService.batchInsertSysWhitePaperSurveyTask(whitePaperRequire,outlineList,whitePaperChapter.getChapterName(),loginUserName,currentDate);
        }

        // 8,将项目信息删除状态置为未删除
        SysWhitePaperProject whitePaperProject = new SysWhitePaperProject();
        whitePaperProject.setId(projectId);
        whitePaperProject.setDelFlag("0");
        sysWhitePaperProjectService.updateSysWhitePaperProject(whitePaperProject);
    }

    /**
     * 修改白皮书章节
     *
     * @param sysWhitePaperChapter 白皮书章节
     * @return 结果
     */
    @Override
    public int updateSysWhitePaperChapter(SysWhitePaperChapter sysWhitePaperChapter)
    {
        sysWhitePaperChapter.setUpdateTime(DateUtils.getNowDate());
        return sysWhitePaperChapterMapper.updateSysWhitePaperChapter(sysWhitePaperChapter);
    }

    /**
     * 批量删除白皮书章节
     *
     * @param ids 需要删除的白皮书章节主键
     * @return 结果
     */
    @Override
    public int deleteSysWhitePaperChapterByIds(Long[] ids)
    {
        return sysWhitePaperChapterMapper.deleteSysWhitePaperChapterByIds(ids);
    }

    /**
     * 删除白皮书章节信息
     *
     * @param id 白皮书章节主键
     * @return 结果
     */
    @Override
    public int deleteSysWhitePaperChapterById(Long id)
    {
        return sysWhitePaperChapterMapper.deleteSysWhitePaperChapterById(id);
    }
}
